/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package convert_crypto;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 *
 * @author suzunghia
 */
public class CryptoFile {

    public static void Encrypt(String pass, String pathInput) {
        try {
            KeySpec ks = new DESKeySpec(pass.getBytes());
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = skf.generateSecret(ks);

            File f = new File(pathInput);        
            String path = f.getName();
            String str = f.getParent() + "/" + path.substring(0, path.indexOf("."));
            // Tao doi tuong cipher
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            FileInputStream in = new FileInputStream(f);
            FileOutputStream out = new FileOutputStream(new File(str + ".en"));

            CipherOutputStream cout = new CipherOutputStream(out, cipher);

            byte[] buffer = new byte[1024];
            int length;
            try {
                while ((length = in.read(buffer)) != -1) {
                    cout.write(buffer, 0, length);
                }
                in.close();

                cout.close();
                System.out.println("Encrypt thanh cong");
            } catch (IOException ex) {
                // TODO Auto-generated catch block
                System.out.println("Wrong something! " + ex.toString());
            }

        } catch (Exception ex) {
            System.out.println("Wrong something! " + ex.toString());
        }         
        File f = new File(pathInput);
        f.delete();
    }

    public static void Decrypt(String pass, String pathInput) {
        try {
            KeySpec ks = new DESKeySpec(pass.getBytes());
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = skf.generateSecret(ks);

            // Tao doi tuong cipher
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            File f = new File(pathInput);
            String path = f.getName();
            String str = f.getParent() + "/" + path.substring(0, path.indexOf(".")) + ".xml";
            File xmlFile = new File(str);
            FileInputStream in = new FileInputStream(f);
            FileOutputStream out = new FileOutputStream(xmlFile);

            CipherOutputStream cout = new CipherOutputStream(out, cipher);

            byte[] buffer = new byte[1024];
            int length;
            try {
                while ((length = in.read(buffer)) != -1) {
                    cout.write(buffer, 0, length);
                }
                in.close();

                cout.close();
                System.out.println("Decrypt thanh cong");
            } catch (IOException ex) {
                // TODO Auto-generated catch block
                System.out.println("Wrong something! " + ex.toString());
            }

        } catch (Exception ex) {
            System.out.println("Wrong something! " + ex.toString());
        }

        //RenameFile(pathInput);

    }
}
